<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '用户管理 - 编辑用户',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(9)>ul").show(80);
    	$(".js-sidebar-nav>li:nth-child(9)>a>span").addClass("sidebar-nav-sub-ico-rotate");
    	$(".js-sidebar-nav>li:nth-child(9)>ul>li:nth-child(1)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">
                                <ol class="am-breadcrumb am-breadcrumb-slash">
                                    <li><a href="/admin/user/index?page=1">用户管理</a></li>
                                    <li class="am-active">编辑用户</li>
                                </ol>
                            </div>
                        </div>
                        <form action="/admin/user/edit"  method="post" id="submitForm" class="am-form am-text-sm">
                            <div class="widget-body  am-fr">
                                <div class="tpl-table-black-operation">
                                    <a href="javascript:void(0);" class="tpl-table-black-operation-del">
                                        基本信息
                                    </a>
                                </div>

                                <div style="margin-top: 20px">
                                    <div class="am-g">
                                        <!-- Row start -->
                                        <div class="am-u-sm-12 am-g-fixed">
                                                <div class="am-u-md-6">
                                                    <!--                                                    <form class="am-form am-text-sm">-->
                                                    <div class="am-u-sm-12 am-form-group">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0 must"
                                                                   for="nickname">姓名</label>
                                                            <div class="am-u-sm-10">
                                                                <input type="text"  id="nickname" name="nickname" value="<?php echo $list['nickname'] ?>" readonly>
                                                            </div>
                                                            
                                                    </div>

                                                    <div class="am-u-sm-12 am-form-group">
                                                            <label class="am-u-md-2 col-sm-centered am-md-text-right am-padding-left-0 must" for="role_id">会员角色</label>
                                                            <div class="am-u-sm-10">
                                                                <button type="button" class="am-btn am-btn-default" id="role_id" data-am-modal="{target: '#selectRole', closeViaDimmer: 0, width: 500, height: 380}" title="<?php echo $list['role_id'] ?>"><?php echo $list['role_id'] ?></button> 
                                                            <input type="hidden" name="role_id" id="role_id_hidden" >
                                                            </div>

                                                            

                                                    </div>

                                                    <div class="am-u-sm-12 am-form-group">
                                                            <label class="am-u-md-2 am-md-text-right am-padding-left-0">性别</label>
                                                            <div class="am-checkbox am-u-md-10">
                                                              <?php
                                                                  if($list['sex']){
                                                                      echo '<label><input name="sex" type="radio" value="0"> 男</label>
                                                                      			&nbsp;&nbsp;&nbsp;&nbsp;
                                                                            <label><input name="sex" type="radio" value="1" checked> 女</label>';
                                                                  }else{
                                                                      echo '<label><input name="sex" type="radio" value="0" checked> 男</label>
                                                                      			&nbsp;&nbsp;&nbsp;&nbsp;
                                                                            <label><input name="sex" type="radio" value="1"> 女</label>';
                                                                  }
                                                              ?>
                                                            </div>
                                                    </div>


                                                    <!--                                                    </form>-->
                                                </div>

                                                <div class="am-u-md-6">
                                                    <!--                                                    <form class="am-form am-text-sm">-->

                                                    <div class="am-u-sm-12 am-form-group">
                                                        <label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="username">用户名称(登录名)</label>
                                                        <div class="am-u-sm-10">
                                                            <input  type="text" id="username" name="username" value="<?php echo $list['username'] ?>" readonly>
                                                        </div>

                                                            
                                                    </div>

                                                    <div class="am-u-sm-12 am-form-group">
                                                        <label class="am-u-md-2 am-md-text-right am-padding-left-0 must" for="password">登录密码</label>
                                                        <div class="am-u-sm-10">
                                                            <input  type="text"  id="password" name="password" value="<?php echo $list['password'] ?>">
                                                        </div>
                                                            
                                                            
                                                    </div>

                                                    <div class="am-u-sm-12 am-form-group">
                                                        <label class="am-u-md-2 am-md-text-right am-padding-left-0" for="phone">手机号码</label>
                                                        <div class="am-u-sm-10">
                                                            <input id="phone"  type="text"  name="phone" value="<?php echo $list['phone'] ?>">
                                                        </div>

                                                            
                                                    </div>
                                                </div>
                                        </div>
                                        <!-- Row end -->
                                    </div>
                                    <hr>
                                </div>

                                <div class="tpl-table-black-operation">
                                    <a href="javascript:void(0);" class="tpl-table-black-operation-del">
                                        其他信息
                                    </a>
                                </div>

                                <div style="margin-top: 20px">
                                    <div class="am-g">
                                        <!-- Row start -->
                                        <div class="am-u-sm-12 am-g-fixed">
                                                        <div class="am-u-md-6">
                                                            <!--                                                    <form class="am-form am-text-sm">-->
                                                            <div class="am-u-sm-12 am-form-group">
                                                                <label class="am-u-md-2 am-md-text-right am-padding-left-0"
                                                                           for="doc-ipt-text-1">所属部门</label>
                                                                <div class="am-u-sm-10">
                                                                    <input  type="text" id="section" name="section" value="<?php echo $list['section'] ?>">
                                                                </div>

                                                                    
                                                            </div>


                                                            <div class="am-u-sm-12 am-form-group">
                                                                <label class="am-u-md-2 am-md-text-right am-padding-left-0"
                                                                           for="residence">居住地址</label>
                                                                <div class="am-u-sm-10">
                                                                    <textarea rows="5" name="residence"
                                                                                  id="residence"><?php echo $list['residence'] ?></textarea>
                                                                </div>

                                                                        
                                                            </div>


                                                            <!--                                                    </form>-->
                                                        </div>

                                                        <div class="am-u-md-6">
                                                            <!--                                                    <form class="am-form am-text-sm">-->

                                                            <div class=" am-u-sm-12 am-form-group">
                                                                <label class="am-u-md-2 am-md-text-right am-padding-left-0"
                                                                           for="name">身份证号码</label>
                                                                <div class="am-u-sm-10">
                                                                    <input type="text" name="card" value="<?php echo $list['card'] ?>">
                                                                </div>

                                                                    
                                                            </div>

                                                            <input type="hidden" name="id" value="<?php echo $list['id']?>">


                                                            <!--                                                    </form>-->
                                                        </div>

                                        </div>
                                        <!-- Row end -->
                                    </div>
                                </div>

                                <div class="btn-box">
                                    <button id="commit" type="submit" class="am-btn am-btn-primary  tpl-btn-bg-color-success " style="width:220px;margin: 20px;">提交</button>

                                    <button onclick="back()" type="button" class="am-btn am-btn-primary tpl-btn-bg-color-success " style="width:220px">取消</button>

                                </div>

                            </div>


                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>
<!-- 模态框 -->
<div class="am-modal am-modal-no-btn" tabindex="-1" id="selectRole">
  <div class="am-modal-dialog">
    <div class="am-modal-hd">选择角色
      <a href="javascript: void(0)" class="am-close am-close-spin" data-am-modal-close>&times;</a>
    </div>
    <div class="am-modal-bd">
        <form class="am-form am-form-horizontal">
            <div class="am-form-group search-box">
                <div class="am-u-sm-8 search-input">
                    <input type="text" id="searchName" placeholder="请输入角色名称查询">
                </div>
                <div class="am-u-sm-4" >
                    <button type="button" class="am-btn am-btn-success" id="find">查询</button>
                    <button type="button" class="am-btn am-btn-primary" id="confirm" data-am-modal-close>确定</button>
                </div>
            </div>
        </form>
        <div class="am-u-sm-12">
            <table class="am-table am-table-striped am-table-hover">
                <thead >
                    <tr >
                        <th style="width: 20%"><input type="checkbox" class="check-all" name=""></th>
                        <th style="width: 20%">序号</th>
                        <th style="width: 60%">角色名称</th>
                    </tr>
                </thead>
                <tbody class="tr-box">
                </tbody>
            </table>
        </div>
        <div class="pagination-box">
                <ul class="am-pagination" id="pagination" >
                  <!-- <li class="am-disabled"><a href="#">&laquo;</a></li>
                  <li class="am-active"><a href="#">1</a></li>
                  <li><a href="#">2</a></li>
                  <li><a href="#">3</a></li>
                  <li><a href="#">4</a></li>
                  <li><a href="#">5</a></li>
                  <li><a href="#">&raquo;</a></li> -->
                </ul>
            </div>
    </div>
  </div>
</div>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/zdy_form.js"></script>
<script>
    function back() {
        window.history.back();
    }
</script>
<script>
    //编辑时  把原来的会员角色给到隐藏的input中
    var editRole = $("#role_id").text();
    $("#role_id_hidden").val(editRole);


    var totalPage = 0;
    var arrSelect = [];
    var arr = [];
    //点击 请选择角色
    $("#role_id").click(function(){
        var data = {
            page:1
        };
        ajaxAdd(data);
    });

    // 请求后台角色
    function ajaxAdd(data){
        $.ajax({
            type:"post",
            url:"/admin/role/roleList",
            data:data,
            dataType:"json",
            success:function(msg){
                //数据
                arr = msg[0];
                changeRoleList();

                //分页
                changeRolePage(msg);
                
                
            }
        });
    }

    function changeRoleList(){
        var tbody = $("#selectRole").find(".tr-box");
        var tr = '';
        for(var i = 0; i < arr.length ; i++){
            var obj = arr[i];
            var isCheck = "";
            for (var j = 0; j < arrSelect.length; j++) {
                if (arrSelect[j].id == obj.id) {
                    isCheck = "checked";
                    break;
                }
            }   
            tr +='<tr>'
                +'<td><input type="checkbox" data-id="'+ arr[i].id +'" class="check-one" name="" '+ isCheck +'></td>'
                +'<td>'+ arr[i].id +'</td>'
                +'<td class="product_no">'+ arr[i].name +'</td>'
            +'</tr>';
        }
        tbody.html(tr);

        isCheckedOfUpper();
    }

    function changeRolePage(msg){
        totalPage = Math.ceil(msg.total_num / msg.pageNum);
        var str = '';
        str += '<li><a href="javascript:void(0)" class="prev-page">&laquo;</a></li>';
        for (var k = 1; k <= totalPage; k++) {
            if(k == msg.this_page){
                str += '<li class="am-active"><a href="javascript:; ">'+ k +'</a></li>';

            }else{
                str += '<li><a href="javascript:; ">'+ k +'</a></li>';

            }
        }
        str += '<li><a href="javascript:void(0)" class="next-page">&raquo;</a></li>';
        $("#pagination").html(str);
    }

    // 判断全选框的状态
    function isCheckedOfUpper() {
        isCheck = $(".check-one:checked").size() == arr.length;
        $(".check-all").prop("checked", isCheck);       
    }

    //全选反选
    $(document).on("click", ".check-all", function() {
        var isCheck = this.checked;
        $(".check-one").each(function() {
            if (isCheck && !this.checked) {
                // 全选, 单个未选中时
                $(this).prop("checked", isCheck);
                var i = $(this).parents("tr").index();
                arrSelect.push(arr[i]);                             
            } else if (!isCheck && this.checked) {
                // 全不选, 单个选中时
                $(this).prop("checked", isCheck);
                var i = $(this).parents("tr").index();
                arrSelect.splice(arrSelect.indexOf(arr[i]), 1);         
            }           
        }); 
        console.log(arrSelect);        
    });
    
    $(document).on("click", ".check-one", function() {
        isCheckedOfUpper();
        
        var i = $(this).parents("tr").index();
        if (this.checked) {             
            arrSelect.push(arr[i]);
        } else {
            for(var k = 0;k < arrSelect.length ; k++){
                if(arrSelect[k].id == $(this).attr("data-id")){
                    arrSelect.splice(k, 1);
                    break;
                }
            }
        }

    });

    //点击分页
    $(document).on("click","#pagination>li>a",function(){
        if($(this).hasClass("prev-page") || $(this).hasClass("next-page") || $(this).parent().hasClass("am-active")){
            return;
        }
        var page = $(this).text();
        var data = {
            page: page
        };
        ajaxAdd(data);
    });
    $(document).on("click","#pagination .prev-page",function(){
        if($(this).parent().next().hasClass("am-active")){
            return;
        }
        var page = $(this).parent().siblings(".am-active").text();
        page--;
        var data = {
            page: page
        };
        ajaxAdd(data);

    });
    $(document).on("click","#pagination .next-page",function(){
        if($(this).parent().prev().hasClass("am-active")){
            return;
        }
        var page = $(this).parent().siblings(".am-active").text();
        page++;
        var data = {
            page: page
        };
        ajaxAdd(data);

    });


    // 选择角色搜索
    $("#find").click(function(){
    var name = $("#searchName").val();
        if(!name){
            alert("请输入角色名称查询。");
        }else{
            var url ="/admin/role/roleList";
            var data ={
                page:1,
                search:name
            };
            ajaxAdd(data);
        }
    });

    //选择角色确定
    $("#confirm").click(function(){
        var role=[];
        var checkedLen = arrSelect.length;
        var roleName;
        for(var i = 0;i < checkedLen;i++){
            roleName = arrSelect[i].name;
            role.push(roleName);
        }
        role = role.join(",");
        role = role.length ? role : "选择角色";
        $("#role_id").text(role).prop("title", role);    
        $("#role_id_hidden").val(role);    

    });


     var lockSubmit = false;
    $("#commit").on("click",function() {    
        if(lockSubmit){
            return false;
        }
        lockSubmit =true;
        var role_id = $("#role_id").text();
        // 表单验证
        var valid = zdy.validate.valid({
            rules:{
                nickname:{required:true},
                username:{required:true},
                password:{required:true}
            }, message:{
                nickname:{required:"请填写姓名"},
                username:{required:"请填写用户名称"},
                password:{required:"请填写登录密码"}
            }   
        });

        if (valid && (role_id == "选择角色" || !role_id)) {
            valid = false;
            var message = "请选择会员角色";
            $("#pop-up-content").text(message);
            $("#my-alert").modal();
        }
        
        if (!valid) {
            lockSubmit = false;
            return false;
        }

    });
    
</script>
</body>

</html>